package com.example.cloudprinter.printer.yilianyun;

import com.example.cloudprinter.api.yilianyun.YilianYunPrinterApi;
import com.example.cloudprinter.network.retrofit.RetrofitUtil;
import com.example.cloudprinter.printer.yilianyun.callback.OnAddCallback;
import com.example.cloudprinter.printer.yilianyun.callback.OnDeleteCallback;
import com.example.cloudprinter.printer.yilianyun.callback.OnPrintCallback;
import com.example.cloudprinter.printer.yilianyun.callback.OnSetVoiceCallback;
import com.example.cloudprinter.printer.yilianyun.callback.OnTokenCallback;
import com.example.cloudprinter.network.RxUtil;

import rx.subscriptions.CompositeSubscription;

import static com.example.cloudprinter.utils.EncodeUtils.getSin;
import static com.example.cloudprinter.utils.EncodeUtils.getuuid;

/**
 * 易联云打印机
 */
public class YilianYunPrinter {
    private static final String host = "https://open-api.10ss.net/";
    private YilianYunPrinterApi api;
    private CompositeSubscription mSubscriptions = new CompositeSubscription();
    //应用id
    private static String client_id = "1046021141";
    //应用secret
    private static String client_secret = "d9e74dd3c5da8f9ff92c36f44785ca94";

    public YilianYunPrinter() {
        api = RetrofitUtil.getService(YilianYunPrinterApi.class, host);
    }

    /**
     * 获取访问令牌
     */
    public void getToken(OnTokenCallback callback) {
        mSubscriptions.add(api.getToken(client_id, "client_credentials", getSin(client_id, client_secret),
                "all", String.valueOf(System.currentTimeMillis() / 1000), getuuid())
                .compose(RxUtil.applySchedulers())
                .subscribe(tokenResponse -> {
                    if (callback != null) {
                        if (tokenResponse.getError() == 0) {
                            callback.onToken(tokenResponse.getBody());
                        } else {
                            callback.onFailure(tokenResponse.getError(), tokenResponse.getError_description());
                        }
                    }
                }, ex -> {
                    if (callback != null)
                        callback.onFailure(-1, ex.getLocalizedMessage());
                }));
    }

    /**
     * 刷新access_token
     *
     * @param refresh_token
     */
    public void refreshToken(String refresh_token, OnTokenCallback callback) {
        mSubscriptions.add(api.refreshToken(client_id, "refresh_token", "all",
                getSin(client_id, client_secret), refresh_token, getuuid(), String.valueOf(System.currentTimeMillis() / 1000))
                .compose(RxUtil.applySchedulers())
                .subscribe(accessTokenResponse -> {
                    if (callback != null) {
                        if (accessTokenResponse.getError() == 0) {
                            callback.onToken(accessTokenResponse.getBody());
                        } else {
                            callback.onFailure(accessTokenResponse.getError(), accessTokenResponse.getError_description());
                        }
                    }
                }, ex -> {
                    if (callback != null)
                        callback.onFailure(-1, ex.getLocalizedMessage());
                }));
    }

    /**
     * 添加打印机
     *
     * @param machine_code 打印机终端号
     * @param msign        打印机密钥
     * @param access_token 访问令牌
     * @param callback
     */
    public void addPrinter(String machine_code, String msign, String access_token, OnAddCallback callback) {
        mSubscriptions.add(api.addPrinter(client_id, machine_code, msign,
                access_token, getSin(client_id, client_secret), getuuid(), String.valueOf(System.currentTimeMillis() / 1000))
                .compose(RxUtil.applySchedulers())
                .subscribe(addPrinterResponse -> {
                    if (callback != null) {
                        if (addPrinterResponse.getError() == 0) {
                            callback.onAddSuccess();
                        } else {
                            callback.onAddFailure(addPrinterResponse.getError(), addPrinterResponse.getError_description());
                        }
                    }
                }, ex -> {
                    if (callback != null)
                        callback.onAddFailure(-1, ex.getLocalizedMessage());
                }));
    }

    /**
     * 扫描二维码快速授权打印机
     *
     * @param machine_code 打印机终端编号
     * @param qr_key       二维码图解析出来的特殊密钥
     * @param callback
     */
    public void qrAuth(String machine_code, String qr_key, OnAddCallback callback) {
        mSubscriptions.add(api.qrAuth(client_id, machine_code, qr_key, "all",
                getSin(client_id, client_secret), getuuid(), String.valueOf(System.currentTimeMillis() / 1000))
                .compose(RxUtil.applySchedulers())
                .subscribe(authResponse -> {

                }, ex -> {

                }));
    }

    /**
     * 删除打印机
     *
     * @param machine_code 打印机终端编号
     * @param access_token 访问令牌
     * @param callback
     */
    public void deletePriter(String machine_code, String access_token, OnDeleteCallback callback) {
        mSubscriptions.add(api.deletePrinter(client_id, machine_code, access_token,
                getSin(client_id, client_secret), getuuid(), String.valueOf(System.currentTimeMillis() / 1000))
                .compose(RxUtil.applySchedulers())
                .subscribe(addPrinterResponse -> {
                    if (callback != null) {
                        if (addPrinterResponse.getError() == 0) {
                            callback.onDeleteSuccess();
                        } else {
                            callback.onDeleteFailure(addPrinterResponse.getError(), addPrinterResponse.getError_description());
                        }
                    }
                }, ex -> {
                    if (callback != null)
                        callback.onDeleteFailure(-1, ex.getLocalizedMessage());
                }));
    }

    /**
     * 设置语音
     *
     * @param machine_code 打印机终端编号
     * @param access_token 访问令牌
     * @param content      播报内容
     * @param volume       音量
     * @param voiceType    声音类型
     * @param callback
     */
    public void setVoice(String machine_code, String access_token, String content, int volume, int voiceType,
                         OnSetVoiceCallback callback) {
        String con = "[\"" + content + "\"," + volume + "," + voiceType + "]";
        mSubscriptions.add(api.setVoice(client_id, access_token, machine_code, con, false, 0,
                getSin(client_id, client_secret), getuuid(), String.valueOf(System.currentTimeMillis() / 1000))
                .compose(RxUtil.applySchedulers())
                .subscribe(setSoundResponse -> {
                    if (callback != null) {
                        if (setSoundResponse.getError() == 0) {
                            callback.onSetSuccess();
                        } else {
                            callback.onSetFailure(setSoundResponse.getError(), setSoundResponse.getError_description());
                        }
                    }
                }, ex -> {
                    if (callback != null)
                        callback.onSetFailure(-1, ex.getLocalizedMessage());
                }));
    }

    /**
     * 打印
     *
     * @param machine_code 打印机终端编号
     * @param access_token 访问令牌
     * @param content      打印内容
     * @param origin_id    商户系统内部订单号
     * @param callback
     */
    public void print(String machine_code, String access_token, String content, String origin_id, OnPrintCallback callback) {
        mSubscriptions.add(api.print(client_id, access_token, machine_code, content, origin_id,
                getSin(client_id, client_secret), getuuid(), String.valueOf(System.currentTimeMillis() / 1000))
                .compose(RxUtil.applySchedulers())
                .subscribe(setSoundResponse -> {
                    if (callback != null) {
                        if (setSoundResponse.getError() == 0) {
                            callback.onPrintSuccess();
                        } else {
                            callback.onPrintFailure(setSoundResponse.getError(), setSoundResponse.getError_description());
                        }
                    }
                }, ex -> {
                    if (callback != null)
                        callback.onPrintFailure(-1, ex.getLocalizedMessage());
                }));
    }

    /**
     * 设置音量
     *
     * @param machine_code  打印机终端编号
     * @param access_token  访问令牌
     * @param response_type 声音类型，蜂鸣器:buzzer,喇叭:horn
     * @param voice         [1,2,3] 3种音量设置
     * @param callback
     */
    public void setSound(String machine_code, String access_token, String response_type, String voice, OnSetVoiceCallback callback) {
        mSubscriptions.add(api.setSound(client_id, access_token, machine_code, response_type, voice,
                getSin(client_id, client_secret), getuuid(), String.valueOf(System.currentTimeMillis() / 1000))
                .compose(RxUtil.applySchedulers())
                .subscribe(setSoundResponse -> {
                    if (callback != null) {
                        if (setSoundResponse.getError() == 0) {
                            callback.onSetSuccess();
                        } else {
                            callback.onSetFailure(setSoundResponse.getError(), setSoundResponse.getError_description());
                        }
                    }
                }, ex -> {
                    if (callback != null)
                        callback.onSetFailure(-1, ex.getLocalizedMessage());
                }));
    }

    /**
     * 移除所有的请求
     */
    public void release() {
        mSubscriptions.clear();
    }

}
